package com.three.iot.service.impl.admin;

import com.three.iot.api.SysUserRoleService;
import com.three.iot.commons.dto.SysUserRoleDto;
import com.three.iot.commons.dto.SysUserRoleResultDto;
import com.three.iot.commons.dto.SysUserRoleSaveDto;
import com.three.iot.commons.enums.Result;
import com.three.iot.jdbc.model.SysRole;
import com.three.iot.jdbc.model.SysUserRole;
import com.three.iot.jdbc.mapper.SysRoleMapper;
import com.three.iot.jdbc.mapper.SysUserRoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author:luiz
 * @Date: 2018/3/9 10:23
 * @Descripton:
 * @Modify :
 **/
@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public Result<List<SysUserRoleResultDto>> findByUserNo(String userNo) {
        List<SysUserRole> sysUserRoleList = sysUserRoleMapper.selectByUserNo(userNo);
        List<SysUserRoleResultDto> sysUserRoleResultDtos = convertDto(sysUserRoleList);
        return Result.newSuccess(sysUserRoleResultDtos);
    }

    private List<SysUserRoleResultDto> convertDto(List<SysUserRole> sysUserRoleList) {
        List<SysUserRoleResultDto> sysUserRoleResultDtos = new ArrayList<>();
        List<SysRole> sysRoles = sysRoleMapper.selectAll();
        for (SysUserRole sysUserRole : sysUserRoleList) {
            SysUserRoleResultDto sysUserRoleResultDto = new SysUserRoleResultDto();
            BeanUtils.copyProperties(sysUserRole, sysUserRoleResultDto);
            //sysUserRoleResultDto.setPId();
            sysUserRoleResultDto.setName(sysUserRole.getRoleName());
            boolean checked = checkIfExists(sysUserRole, sysRoles);
            sysUserRoleResultDto.setChecked(checked);
            sysUserRoleResultDtos.add(sysUserRoleResultDto);
        }
        return sysUserRoleResultDtos;

    }

    private boolean checkIfExists(SysUserRole sysUserRole, List<SysRole> sysRoles) {
        for (SysRole sysRole : sysRoles) {
            if (sysRole.getRoleCode().equals(sysUserRole.getRoleCode())) {
                return true;
            }
        }
        return false;
    }

    public Result deleteByIds(List<String> ids) {
        List<SysUserRole> sysUserRoles = new ArrayList<>();
        for (String id : ids) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setId(Integer.valueOf(id));
            sysUserRoles.add(sysUserRole);
        }
        sysUserRoleMapper.deleteInBatch(sysUserRoles);
        return Result.newSuccess();
    }

    public Result delete(String id) {
        sysUserRoleMapper.deleteByPrimaryKey(Integer.valueOf(id));
        return Result.newSuccess();
    }

    @Override
    public Result save(SysUserRoleSaveDto sysUserRoleSaveDto) {
        deleteByUserNo(sysUserRoleSaveDto.getUserNo());
        List<SysUserRoleDto> sysUserRoleDtos = sysUserRoleSaveDto.getSysUserRoleDtoList();
        for (SysUserRoleDto sysUserRoleDto : sysUserRoleDtos) {
            SysUserRole sysUserRole = new SysUserRole();
            BeanUtils.copyProperties(sysUserRoleDto, sysUserRole);
            sysUserRole.setUserNo(sysUserRoleSaveDto.getUserNo());
            sysUserRole.setCreateNo(sysUserRoleSaveDto.getOperUserNo());
            sysUserRole.setCreateTime(new Date());
            sysUserRoleMapper.insertSelective(sysUserRole);
        }
        return Result.newSuccess();
    }

    private void deleteByUserNo(String userNo) {
        sysUserRoleMapper.deleteByUserNo(userNo);
    }


}
